package com.zoro.barn.business.service.system.business.systeminfo.controller;

import com.zoro.barn.business.service.system.business.systeminfo.entity.BarnSystemInfo;
import com.zoro.barn.business.service.system.business.systeminfo.service.BarnSystemInfoService;
import com.zoro.barn.business.service.system.exceptions.DataUncompletedException;
import com.zoro.barn.commons.webcommons.tools.RequestUser;
import com.zoro.barn.commons.webcommons.tools.WebContentUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhaoxingwu
 */
@RestController
@RequestMapping("/systemInfo")
public class BarnSystemInfoController {

    private final BarnSystemInfoService barnSystemInfoService;

    public BarnSystemInfoController(BarnSystemInfoService barnSystemInfoService) {
        this.barnSystemInfoService = barnSystemInfoService;
    }

    /**
     * 保存
     *
     * @param barnSystemInfo data
     * @return saved data
     */
    @RequestMapping("/save")
    public BarnSystemInfo save(@RequestBody BarnSystemInfo barnSystemInfo) {
        RequestUser user = WebContentUtil.requestUserInfo();
        barnSystemInfo.setUpdateId(user.getPid());
        if (StringUtils.isAnyBlank(barnSystemInfo.getPid(), barnSystemInfo.getVariableName(), barnSystemInfo.getVariableValue())) {
            throw new DataUncompletedException();
        }
        return this.barnSystemInfoService.save(barnSystemInfo);
    }

    /**
     * 查询
     *
     * @param barnSystemInfo 查询条件
     * @return result set
     */
    @RequestMapping("/query")
    public List<BarnSystemInfo> query(BarnSystemInfo barnSystemInfo) {
        return this.barnSystemInfoService.query(barnSystemInfo);
    }

    @RequestMapping("/getById")
    public BarnSystemInfo getById(String pid) {
        return this.barnSystemInfoService.getById(pid);
    }
}
